<?php
namespace zhtfrontend\models\forms;
use common\components\extenders\MenuManage;
use zhtfrontend\models\datas\AuthItem;
use yii\base\Model;
use yii\db\Query;

class RoleForm extends Model
{
    public $name;
    public $description;



    public function rules()
    {
        return [
            [['name', 'description'], 'required'],
            [['name'], 'check_role','on'=>'create'],
            [['description'], 'safe'],
        ];
    }
    public function attributeLabels()
    {
        return [
            'name' => \Yii::t('zhtfrontend', '角色名称'),
            'description' => \Yii::t('zhtfrontend', '角色描述'),
        ];
    }

//    public function fields()
//    {
//        $fields = parent::fields();
//        $fields['status'] = function () {
//            switch ($this->status) {
//                case 0:
//                    return \Yii::t('zhtfrontend', '已停用');
//                case 1:
//                    return \Yii::t('zhtfrontend', '已激活');
//            }
//        };
//        $fields['created_at'] = function () {
//            return \Yii::$app->formatter->asDatetime($this->created_at);
//        };
//        unset($fields['password']);
//        return $fields;
//    }

    public function extraFields()
    {
        return [
            'operate' => function () {
                return (new MenuManage(MenuManage::RECORD_ITEM_TOOLBAR, $this->attributes))->create();
            },
        ];
    }



    public function check_role($attribute){
//        $query=new Query();
//        $query->select('*')->from('{{%auth_item}}');
//        $query->where(['name'=>$this->$attribute]);
//        $row=$query->one();
        $row = AuthItem::find()->where(['name'=>$this->$attribute])->one();
        if($row){
            $this->addError($attribute, '角色名称已存在');
        }
    }
}
